package uni.UNIBEE7B8D.Adapter.study;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import uni.UNIBEE7B8D.Adapter.KechengClickOnListener;
import uni.UNIBEE7B8D.Adapter.RecycClickOnListener;
import uni.UNIBEE7B8D.Enty.CommonClass;
import uni.UNIBEE7B8D.Enty.HomeKecheng;
import uni.UNIBEE7B8D.R;

public class TuijianRecycAdapter extends RecyclerView.Adapter<TuijianRecycAdapter.MyViewHolder> {
    private List<HomeKecheng.ResultBean> fruitList;
    private Context context;
    private KechengClickOnListener recycClickOnListener;

    public KechengClickOnListener getRecycClickOnListener() {
        return recycClickOnListener;
    }

    public void setRecycClickOnListener(KechengClickOnListener recycClickOnListener) {
        this.recycClickOnListener = recycClickOnListener;
    }

    public TuijianRecycAdapter(List<HomeKecheng.ResultBean> fruitList, Context context) {
        this.fruitList = fruitList;
        this.context = context;
    }

    @NonNull
    @Override
    public TuijianRecycAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_tuijian_view, parent, false);
        final TuijianRecycAdapter.MyViewHolder myViewHolder = new TuijianRecycAdapter.MyViewHolder(itemView);
        return myViewHolder;
    }

//    @NonNull
//    @Override
//    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        return null;
//    }

    @Override
    public void onBindViewHolder(@NonNull TuijianRecycAdapter.MyViewHolder holder, int position) {
        HomeKecheng.ResultBean horItem = fruitList.get(position);
        RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(20));//图片圆角为20
        Glide.with(context).load(horItem.getCoverUrl()).apply(options).error(R.mipmap.ic_launcher).into(holder.image);
        holder.cost.setText("￥"+horItem.getCost()+"元");
        holder.memberCost.setText("会员价￥"+horItem.getMemberCost()+"元");
        holder.title.setText(horItem.getName());
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeKecheng.ResultBean fruit = fruitList.get(position);
                recycClickOnListener.onItemClick(fruit);
            }
        });
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        View fruitView;

        TextView title,cost,memberCost;
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            fruitView = itemView;
            title = itemView.findViewById(R.id.title);
            image = itemView.findViewById(R.id.image);
            cost = itemView.findViewById(R.id.cost);
            memberCost = itemView.findViewById(R.id.memberCost);
        }
    }

    @Override
    public int getItemCount() {
        return fruitList.size();
    }
}
